package com.tc.kqdemo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tc.kqdemo.pojo.Data;
import com.tc.kqdemo.service.DataService;
import com.tc.kqdemo.service.DingDing;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/pro")
public class DataCtr {
    @Autowired
    private DataService dataService;


    /*考勤打卡数据:
    (一)保存接收的需要写入的钉钉考勤打卡数据,防止写入钉钉考勤数据失败,以便可以重新再次写入失败的数据
    * (二)保存数据完成之后,开始向外网服务器传输保存的数据    <=======定时任务
    * (三)外网服务器向钉钉考勤写入数据;(1:md5签名;2:http post请求 body为json格式数据)
    * */

    //内网服务器接收海威传输的数据
    @RequestMapping("/receive")
    public JSONObject connTodd(HttpServletRequest req){
        System.out.println(req.getRequestURL()+"<----------receive");
        System.out.println("接收推送的数据开始!!!!"+new Date());
        String data2=null;//传输的数据
        //接收海威传输的数据
        try {
            InputStreamReader reader = new InputStreamReader(req.getInputStream(), "UTF-8");
            char[] buff = new char[10000];
            int len=0;
            System.out.println("http---post请求---body传输数据");
            while((len=reader.read(buff))!=-1){
                data2 = new String(buff, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (req.getInputStream()!=null){
                    req.getInputStream().close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(data2+"<---------接收到的数据data2");
        SimpleDateFormat d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat d2 = new SimpleDateFormat("yyyy-MM-dd");
        String date2=null;
        int num_=0;
        if (data2!=null){
            data2=data2.trim();
            JSONObject jsonObject = JSON.parseObject(data2);
            if (jsonObject.getInteger("code")==0){
                JSONArray dataArr = jsonObject.getJSONArray("data");
                if (dataArr.size()>0){
                    for (int i = 0; i < dataArr.size(); i++) {
                        JSONObject map = JSON.parseObject(dataArr.get(i).toString());
                        Integer sendId = map.getInteger("userId");//传输的userId
                        String tele = map.getString("phone");//手机号码
                        String name2 = map.getString("name");//传输的员工姓名
                        String userid="";
                        String name ="";
                        Long time = map.getLong("time");
                        Date dateTime =new Date(time);//刷脸打卡的时间
                        if (date2==null){
                            date2=d2.format(dateTime);
                        }
                        if (i==0){
                            num_=dataService.checkMaxNumByDate_(date2);
                        }
                        String type =""; //刷脸打卡的类型 :1上班    2下班    3
                        String pic_address = map.getString("picture");//照片的地址
                        String equipid = map.getString("equipId");//设备id
                        String address="国华太仓发电有限公司";//打卡的地址
                        System.out.println("传输过来的姓名:"+name2+",钉钉里面的姓名:"+name+"<======"+i);
                        Data d = new Data();
                        d.setSendId(sendId);
                        d.setSendName(name2);
                        d.setNum_(num_+1);
                        d.setAddress_(address);
                        d.setComment(1);
                        d.setDate_(d2.format(dateTime));
                        d.setEquipid(equipid);
                        d.setName(name);
                        d.setPhone(tele);
                        d.setPic_address(pic_address);
                        d.setType(type);
                        d.setTime(d1.format(dateTime));
                        d.setTime2(dateTime);
                        d.setUserid(userid);
                        d.setIsSuccess("false");
                        d.setIsSend("false");
                        //保存传输的数据
                        boolean s=dataService.saveData(d);
                        if (s){
                            System.out.println("保存接收的数据成功!!!!!-->"+userid);
                        }else{
                            System.out.println("保存接收的数据失败!!!!--->"+userid);
                        }
                    }
                }
            }
        }
        System.out.println("传输的数据本地接收保存结束!!!!!");
        JSONObject res = new JSONObject();
        res.put("msg","数据接收写入钉钉考勤打卡成功");
        res.put("code",0);
        System.out.println("接收推送的数据,数据保存到内网服务器结束!!!!"+new Date());
        return res;
    }




}
